/*
 * 文件名:AuthorizationInterceptor.java
 * 创建时间:2010-03-10
 * 版本:1.0
 * 版权所有:杨涛
 */
package com.yangtao.framework.web.intercepter;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.yangtao.framework.common.SystemConstant;
import com.yangtao.framework.service.ServiceLocator;
import com.yangtao.framework.util.CookieUtil;
import com.yangtao.framework.web.struts2.BaseAction;
import com.yangtao.framework.web.struts2.BaseModel;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 权限拦截器，从Session中取出当前用户，放入model中
 * </p>
 *
 * @author 杨涛
 * @version 1.0
 */
public class AuthorizeInterceptor extends AbstractInterceptor {

	private JedisPool jedisPool;

	private int expireSecondes = 3600 * 2;

	public void init() {
		if (jedisPool == null) {
			jedisPool = ServiceLocator.getInstance().getService(JedisPool.class);
		}
	}

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation action) throws Exception {
		Jedis jedisClient = getJedisClient();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			// 如果session中没有用户，或者session中用户不是系统定义的用户，直接返回登陆画面
			String sessionId = request.getParameter(SystemConstant.Y_SESSIONID);
			if (StringUtils.isBlank(sessionId)) {
				sessionId = CookieUtil.getCookieValue(request, SystemConstant.Y_SESSIONID);
			}
			//sessionId不存在
			if (StringUtils.isBlank(sessionId) || !jedisClient.exists(sessionId)) {
				return "login";
			}
			if (action.getAction() instanceof BaseAction<?, ?>) {
				BaseAction<?, ?> ba = (BaseAction<?, ?>) action.getAction();
				BaseModel model = ba.getModel();
				// 设置属性
				Map<String, String> userData = jedisClient.hgetAll(sessionId);
				model.setCompanyId(userData.get(SystemConstant.COMPANY_ID));
				model.setUserId(userData.get(SystemConstant.USER_ID));
				model.setUserName(userData.get(SystemConstant.USER_NAME));
				model.setOrganizationId(userData.get(SystemConstant.ORGANIZATION_ID));
				model.setUserType(userData.get(SystemConstant.USER_TYPE));
				//设置过期时间
				jedisClient.expire(sessionId, expireSecondes);
			}
			return action.invoke();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnJedis(jedisClient);
		}
		return null;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	/**
	 * 取得jedis客户端
	 *
	 * @return jedis客户端
	 */
	public Jedis getJedisClient() {
		Jedis client = jedisPool.getResource();
		if (!client.isConnected()) {
			client.connect();
		}
		return client;
	}

	/**
	 * 将jedis连接返还给连接池
	 *
	 * @param jedis
	 */
	public void returnJedis(Jedis jedis) {
		jedisPool.returnResource(jedis);
	}

	public void setExpireSecondes(int expireSecondes) {
		this.expireSecondes = expireSecondes;
	}
}
